//
//  CircleTableViewCell.m
//  Lucky28
//
//  Created by zr on 2018/8/1.
//  Copyright © 2018年 zr. All rights reserved.
//

#import "CircleTableViewCell.h"


@interface CircleTableViewCell ()


@property (nonatomic, weak)UILabel * titleLabel;
@property (nonatomic, weak)UILabel * personLabel;
@property (nonatomic, weak)UILabel * timeLabel;
@property (nonatomic, weak)UIView * lineView;

@end

@implementation CircleTableViewCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self setupUI];
    }
    
    return self;
}


- (void)setModel:(CircleModel *)model{
    
    _model = model;
    
    self.titleLabel.text = [NSString stringWithFormat:@"【%@】%@",model.title,model.content];
    self.personLabel.text = [NSString stringWithFormat:@"%@",model.username];
    
    // timeStampString 是服务器返回的13位时间戳
    NSString *timeStampString  = model.ctime;
    
    // iOS 生成的时间戳是10位
    NSTimeInterval interval    =[timeStampString doubleValue];
    NSDate *date               = [NSDate dateWithTimeIntervalSince1970:interval];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSString *dateString       = [formatter stringFromDate: date];
//    NSLog(@"服务器返回的时间戳对应的时间是:%@",dateString);
    
    self.timeLabel.text = dateString;
    self.ID = model.ID;
}


- (void)setupUI{
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.backgroundColor = [UIColor whiteColor];
    
    
    UILabel * titleLabel = [UILabel new];
    
    titleLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    titleLabel.textColor = [UIColor blackColor];
    titleLabel.numberOfLines = 2;
//    titleLabel.text = @"啊速度和比才发了十八洞村V领上班大V阿拉斯大幅我六点半是阿里速发GVisU盾不出来 啊户撒上了地方VB爱卢浮宫via固定费是拉到身边辣豆腐爱睡觉了就是不好的V领上班大V爱是彼此覅路埃尔文";
    
    self.titleLabel = titleLabel;
    
    [self.contentView addSubview:self.titleLabel];
    
    
    UILabel * explationLabel = [UILabel new];
    
    explationLabel.font = [UIFont systemFontOfSize:13 * SCREENH6];
    explationLabel.textColor = [UIColor whiteColor];
    explationLabel.backgroundColor = MAINCOLOR;
    
    explationLabel.text = @" 置顶 ";
    
    self.explationLabel = explationLabel;
    
    [self.contentView addSubview:self.explationLabel];
    
    
    UILabel * personLabel = [UILabel new];
    
    personLabel.font = [UIFont systemFontOfSize:13 * SCREENH6];
    personLabel.textColor = ColorWithRGB(0xa9a9a9);
    personLabel.text = @"-----";
    
    self.personLabel = personLabel;
    
    [self.contentView addSubview:self.personLabel];

    
    UILabel * timeLabel = [UILabel new];
    
    timeLabel.font = [UIFont systemFontOfSize:13 * SCREENH6];
    timeLabel.textColor = ColorWithRGB(0xa9a9a9);
//    timeLabel.text = @"2018-06-12";
    
    self.timeLabel = timeLabel;
    
    [self.contentView addSubview:self.timeLabel];
    
    UIView * lineView = [UIView new];
    
    lineView.backgroundColor = ColorWithRGB(0xa9a9a9);
    
    self.lineView = lineView;
    
    [self.contentView addSubview:self.lineView];
}


- (void)layoutSubviews{
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.contentView).offset(15 * SCREENH6);
        make.trailing.mas_equalTo(self.contentView).offset(-15 * SCREENH6);
        make.top.mas_equalTo(self.contentView).offset(10 * SCREENH6);
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.bottom.mas_equalTo(self.contentView);
        make.height.mas_equalTo(0.5 * SCREENH6);
    }];
    
    [self.explationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.titleLabel);
        make.bottom.mas_equalTo(self.lineView.mas_top).offset(-10 * SCREENH6);
    }];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.titleLabel);
        make.centerY.mas_equalTo(self.explationLabel);
    }];
    
    [self.personLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.timeLabel.mas_leading).offset(-10 * SCREENH6);
        make.centerY.mas_equalTo(self.timeLabel);
    }];
    
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
